package com.zisuyi.uilibrary.module.activty.present

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.weilele.mvvm.base.helper.LiveDataWrap
import com.weilele.mvvm.base.helper.StatusValue
import com.weilele.mvvm.base.helper.annotation.MvvmVLayoutAdapter
import com.weilele.mvvm.base.helper.annotation.MvvmViewModel
import com.weilele.mvvm.base.helper.wrapObserver
import com.weilele.mvvm.utils.activity.finisSomeActivity
import com.weilele.mvvm.utils.activity.getResString
import com.weilele.mvvm.utils.activity.navigateTo
import com.weilele.mvvm.utils.vLaout.VLayoutAdapter
import com.zisuyi.game.sdk.net.bean.RedeemHistoryBean
import com.zisuyi.uilibrary.R
import com.zisuyi.uilibrary.base.BaseUiActivity
import com.zisuyi.uilibrary.databinding.ActivityPresentBinding
import com.zisuyi.uilibrary.module.activty.redeem.RedeemActivity
import com.zisuyi.uilibrary.module.activty.redeem.RedeemViewModel


class PresentActivity : BaseUiActivity<ActivityPresentBinding>() {
    companion object {
        fun start(appCompatActivity: AppCompatActivity?) {
            appCompatActivity?.navigateTo<PresentActivity>()
        }
    }

    @MvvmViewModel
    private lateinit var redeemViewModel: RedeemViewModel

    override fun getTitleBarTitle(): CharSequence? {
        return getResString(R.string.present)
    }

    @MvvmVLayoutAdapter
    private lateinit var presentHolder: VLayoutAdapter<RedeemHistoryBean, PresentHolder>
    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.rvPresent.addAdapter(presentHolder)
    }

    override fun initData() {
        super.initData()
        redeemViewModel.redeemHistory()
    }

    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(
            redeemViewModel.redeemHistoryData wrapObserver ::onRedeemHistory
        )
    }

    private fun onRedeemHistory(value: StatusValue<MutableList<RedeemHistoryBean>>?) {
        when (value) {
            is StatusValue.Running -> {
                showLoadingView()
            }
            is StatusValue.Error -> {
                showRetryUi(value.nonNullMsg)
            }
            is StatusValue.Success -> {
                showContentView()
                presentHolder.refresh(value.nonNullValue)
            }
        }
    }


    private fun showRetryUi(msg: String?) {
        showOtherView(msg, retryText = "PLAY GAMES") {
            finisSomeActivity(RedeemActivity::class.java)
            finish()
        }
    }
}